package cn.com.gensoft.app.core.mybatis;

import cn.com.gensoft.app.core.interceptor.CameHumpInterceptor;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.spring.boot.starter.SpringBootVFS;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * mybatis plus 配置
 * @author hlj
 */
@Configuration
public class MybatisPlusConfig {

    @Autowired
    private DataSource dataSource;

    @Bean("sqlSessionFactory")
    @ConfigurationProperties(prefix = "mybatis-plus")
    public MybatisSqlSessionFactoryBean sqlSessionFactory() {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        //数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        /*TypeHandler[] typeHandlers={new DictTypeHandler()};
        sqlSessionFactoryBean.setTypeHandlers(typeHandlers);*/
        //全局配置
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        //主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
        globalConfig.setIdType(3);
        //字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
        globalConfig.setFieldStrategy(1);
        //数据库大写下划线转换
        globalConfig.setCapitalMode(true);
        //驼峰下划线转换
        globalConfig.setDbColumnUnderline(true);
        globalConfig.setDbType(DbType.ORACLE.getValue());
        //#填充策略接口
        globalConfig.setMetaObjectHandler(new CommonMetaObjectHandler());
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);
        //mybatis配置
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        //开启驼峰转换
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        //缓存
        mybatisConfiguration.setCacheEnabled(false);
        //配置JdbcTypeForNull
        mybatisConfiguration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor(),cameHumpInterceptor()});
        return sqlSessionFactoryBean;
    }

    /**
     * @Description : 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        //CachePaginationInterceptor page = new CachePaginationInterceptor();
        return page;
    }

    @Bean
    public CameHumpInterceptor cameHumpInterceptor() {
        CameHumpInterceptor cameHumpInterceptor = new CameHumpInterceptor();
        return cameHumpInterceptor;
    }
}
